package ControlLayer;

import java.util.LinkedList;

import DBLayer.DBRBLine;
import DBLayer.IFDBRBLine;
import ModelLayer.RoomBooking;
import ModelLayer.RoomBookingLine;
import ModelLayer.RoomType;

public class RBLineCtr {
	private RBDateCtr rbDateCtr;

	public RBLineCtr() {
		rbDateCtr = new RBDateCtr();
	}

	public int insertLine(int rbId, int rtId, int quantity, double totalPrice) {
		RoomBookingLine line = new RoomBookingLine();
		line.setRoomBooking(new RoomBooking(rbId));
		line.setRoomType(new RoomType(rtId));
		line.setQuantity(quantity);
		line.setTotalPrice(totalPrice);
		IFDBRBLine dbLine = new DBRBLine();
		return dbLine.insertLine(line);
	}

	public int updateLine(int rbId, int lineId, int rtId, int quantity,
			double totalPrice) {
		RoomBookingLine line = new RoomBookingLine();
		line.setRoomBooking(new RoomBooking(rbId));
		line.setId(lineId);
		line.setRoomType(new RoomType(rtId));
		line.setQuantity(quantity);
		line.setTotalPrice(totalPrice);
		IFDBRBLine dbLine = new DBRBLine();
		return dbLine.updateLine(line);
	}

	public void insertAllLineForRB(RoomBooking newRB)
			throws RoomBookingException {

		LinkedList<RoomBookingLine> newlines = newRB.getLines();
		for (RoomBookingLine rbl : newlines) {
			rbDateCtr
					.decrementNrOfAvaiRoom(rbl.getRoomType().getId(),
							newRB.getStartDate(), newRB.getEndDate(),
							rbl.getQuantity());
			if (insertLine(newRB.getId(), rbl.getRoomType().getId(),
					rbl.getQuantity(), rbl.getTotalPrice()) == -1) {
			}
		}

	}

	public RoomBookingLine getLine(int rbId, int lineId) {
		IFDBRBLine dbLine = new DBRBLine();
		return dbLine.getLine(rbId, lineId, true);
	}

	// delete all room booking lines for one room booking
	public int deleteAllLineForRB(int rbId) {
		int rc = -1;
		IFDBRBLine dbLine = new DBRBLine();
		rc = dbLine.deleteLinesForRB(rbId);
		// TODO increase booking year
		return rc;
	}
}
